<?php
/***************************************************************
*  DYNAMO - Simple yet powerful CMS - www.dynamocms.com
*  Copyright (c) M31 2003-2008 - www.m31web.com
*
*  This script is part of the DYNAMO project. The DYNAMO
*  project is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html or included in the distribution
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
*
*  File Name: xxx.php
*      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
* 	
*  File Authors:
*      1.0 : Creation by Jean-Francois Faudi
* 
***************************************************************/


	// taille des zones de texte
	if ($ta_cols == 0)
		$ta_cols = 60;
	if ($ta_rows == 0)
		$ta_rows = 4;

	// Objets RECORD / ARTICLE
	require_once("../../cms/scripting/record.inc.php");

	// conversion du texte
	function text2html($string)
	{
		$string = stripslashes($string);
		$string = ereg_replace(39, 146, $string);
		$string = ereg_replace(10, "<br>", $string);
		$string = ereg_replace(13, "", $string);
		return $string;
	}

	function html2text($string)
	{
		$string = ereg_replace("<br>", 10, $string);
		return $string;
	}

	$id = intVal($id);
	if ($what == "delete")
	{
		// teste si l'identifiant interne est nul
		if ($id != 0)
		{
			$query = "DELETE FROM " . $table_name . " WHERE id = " . $id;
			$mysql_result = database_query($query);
			$id = 0;
		}
		
		$message = "L'enregistrement a &eacute;t&eacute; effac&eacute;.";
	}
	
	if ($what == "modify")
	{
		// teste si l'identifiant interne est nul
		if ($id == 0)
		{
			$max = 1000;
			$query = "SELECT MAX(id) AS max FROM " . $table_name;
			$mysql_result = database_query($query);
			$row = mysql_fetch_object($mysql_result);
			if ($row->max > $max)
				$max = $row->max;
			$id = $max + 1;
		}

		$fields = "";
		reset($fieldlist);
		while (list($key, $fname) = each($fieldlist))
		{
			if (is_array($fname))
				if ($fname["type"] == "none")
					continue;
			$fields .= $key . ", ";
		}
		$fields = substr($fields, 0, -2);
		
		$values = "";
		reset($fieldlist);
		while (list($key, $fname) = each($fieldlist))
		{
			if (is_array($fname))
			{
				if ($fname["type"] == "none")
					continue;
				else if ($fname["type"] == "date")
				{
					$dummy = explode("/", $$key);
					$value = mktime(0, 0, 0, $dummy[1], $dummy[0], $dummy[2]);
				}
				else
					$value = text2html($$key);
			}
			else
				$value = text2html($$key);

			$values .= "'" . $value . "', ";
		}
		$values = substr($values, 0, -2);

		$query = "REPLACE INTO " . $table_name . " (" . $fields . ") VALUES (" . $values . ")"; 
		$mysql_result = database_query($query);
	}

	if ($message) 
		print "<p class=\"page_warning\">$message</p>"; 
	else
		print "<br>";

	if (($id != 0) or ($mode == "edit"))
	{
		if ($mode == "edit")
			Record::modifyTableBegin();
		else
			Record::printTableBegin();
			
		$query = "SELECT * FROM " . $table_name . " WHERE id=" . $id;
		$mysql_result = database_query($query);
		$row = mysql_fetch_array($mysql_result);
		if (($row == 0) and ($id != 0))
			$row["id"] = $id;
		$cur = $row[$orderby];

		if ($orderby != "")
		{
			$query = "SELECT id FROM " . $table_name . " WHERE " . $orderby . " > '" . mysql_real_escape_string($cur) . "' ORDER BY " . $orderby . " LIMIT 0, 1";
			$mysql_result = database_query($query);
			$row2 = mysql_fetch_object($mysql_result);
			$next = intval($row2->id);

			$query = "SELECT id FROM " . $table_name . " WHERE " . $orderby . " < '" . mysql_real_escape_string($cur) . "' ORDER BY " . $orderby . " DESC LIMIT 0, 1";
			$mysql_result = database_query($query);
			$row2 = mysql_fetch_object($mysql_result);
			$prev = intval($row2->id);
		}
		
		print "<input type=\"hidden\" name=\"orderby\" value=\"" . $orderby . "\">";
		if ($mode == "edit")
			print "<input type=\"hidden\" name=\"mode\" value=\"edit\">";

		if (($row) or ($mode == "edit"))
		{		
			if ($orderby != "")
			{
				if ($prev != 0)
					print "<tr><td align=\"left\"><a href=\"?mode=" . $mode . "&orderby=" . $orderby . "&id=" . $prev . "\"><img src=\"../../cms/images/prev.gif\" border=\"0\"></a></td>";
				else
					print "<td></td>";
				if ($next != 0)
					print "<td align=\"right\"><a href=\"?mode=" . $mode . "&orderby=" . $orderby . "&id=" . $next . "\"><img src=\"../../cms/images/next.gif\" border=\"0\"></a></td></tr>";
				else
					print "<td></td>";
			}
			
			reset($fieldlist);
			while (list($key, $fname) = each($fieldlist))
			{
				if ($mode == "edit")
				{
					if (is_array($fname))
					{
						if ($fname["type"] == "select")
							Record::modifyTableSelect($fname["title"], $key, $fname["options"], $row[$key], "", "", 0, 1, 0);
						else if ($fname["type"] == "radio")
							Record::modifyTableRadio($fname["title"], $key, $fname["options"], $row[$key]);
						else if ($fname["type"] == "check")
							Record::modifyTableCheckbox($fname["title"], $key, $row[$key], "1");
						else if ($fname["type"] == "date")
							Record::modifyTableDate($fname["title"], $key, $row[$key]);
						else if ($fname["type"] == "comment")
							Record::modifyTableComment($fname["title"], $key, html2text($row[$key]), "", $ta_cols, $ta_rows);
						else if ($fname["type"] == "none")
							Record::printTableSeparator($fname["title"]);
					}
					else if ($fname != "")
						Record::modifyTableText($fname, $key, $row[$key]);
					else
						print "<input type=\"hidden\" name=\"" . $key . "\" value=\"" . $row[$key] . "\">";
				}
				else
				{
					if (is_array($fname))
					{
						if (($fname["type"] == "select") || ($fname["type"] == "radio") || ($fname["type"] == "check"))
							Record::printTableText($fname["title"], $fname["options"][$row[$key]]);
						else if ($fname["type"] == "date")
							Record::printTableDate($fname["title"], $row[$key]);
						else if ($fname["type"] == "comment")
							Record::printTableText($fname["title"], $row[$key]);
						else if ($fname["type"] == "none")
							Record::printTableSeparator($fname["title"]);
					}
					else if ($fname != "")
						Record::printTableText($fname, $row[$key]);
				}
			}
		}
				
		if ($mode == "edit")
			Record::modifyTableEnd();
		else
			Record::printTableEnd();

	}
				
 ?>